<template>
  <div>
    <el-form class="input-form"
      label-position="right"
      label-width="120px"
      ref="editForm"
      :rules="propRules"
      :disabled="propReadonly"
      :model="form"
    >
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="仓库名称" prop="warehouseName">
          <el-select v-model="form.warehouseId" filterable placeholder="请选择入库仓库">
            <el-option v-for="item in propDictionaries.warehouseId"
              :key="item.id"
              :label="item.name"
              :value="item.id"/>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="品名" prop="matrName">
          <matr-select
            v-model="form.matrId"
            :propRows="propDictionaries.matrId"
            :propDisabled="propReadonly"
            @select="handleMatrSelect"
            ref="matrExtSelect"
          />
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="品规" prop="matrSpec">
          <el-input v-model="form.matrSpec" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="单位" prop="skuUnit">
          <el-input v-model="form.skuUnit" />
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="安全库存" prop="skuQtySafety">
          <el-input-number v-model="form.skuQtySafety" :min="0" :max="maxQty" label="安全库存"></el-input-number>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="最佳库存" prop="skuQtyPerfect">
          <el-input-number v-model="form.skuQtyPerfect" :min="1" :max="maxQty" label="最佳库存"></el-input-number>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="日消耗量" prop="skuQtyDaily">
          <el-input-number v-model="form.skuQtyDaily" :min="0" :max="maxQty" label="日消耗量"></el-input-number>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="要求单位" prop="requiredUnit">
          <matr-unit-select
              ref="matrUnit"
              v-model="form.requiredUnit"
              propUrl="/base/matrUnit/list"
              @select="handleRequiredChange"
              :propMatrId="form.matrId"
              :value="form.requiredUnit"
              propValueField="unit"
              :propDisabled="strings.isBlank(form.matrId)"
            />
        </el-form-item>
      </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import MatrSelect from '@/components/base/MatrSelect'
import MatrUnitSelect from '@/components/base/MatrUnitSelect'

export default {
  data() {
    return {
      form: {},
      viewData: {},
      selectMatrColumns: [
        { name: 'name', title: '品名', span: 6, search: true  },
        { name: 'spec', title: '品规', span: 6, search: true  },
        { name: 'mfgName', title: '厂商名称', span: 6, search: true  },
      ],
      maxQty: 999
    }
  },
  methods: {
    async handleMatrSelect(item) {
      let {id, name, spec, skuUnit, skuPrice} = item
      this.$set(this.form, 'matrId', id)
      this.$set(this.form, 'matrName', name)
      this.$set(this.form, 'matrSpec', spec)
      this.$set(this.form, 'skuUnit', skuUnit)
      this.$set(this.form, 'skuPrice', skuPrice)

      this.$nextTick(async () => {
        await this.$refs.matrUnit.fetchData()
        this.$set(this.form, 'requiredUnit', skuUnit)
        this.$set(this.form, 'requiredRate', 1)
      })
    },
    handleRequiredChange(item) {
      if (!item) {
        this.$set(this.form, 'requiredUnit', this.form.skuUnit)
        this.$set(this.form, 'requiredRate', 1)
        return
      }
      this.$set(this.form, 'requiredUnit', item.unit)
      this.$set(this.form, 'requiredRate', item.rate)
    }
  },
  created() {
    this.$emit('event-register', {
      validate: (callback) => {
        this.$refs.editForm.validate(callback)
      },
      beforeSave: () => {
        if (this.form.skuQtyPerfect < this.form.skuQtySafety) {
          this.utilMessage.error('最佳库存不能小于安全库存')
          return false
        }

        let warehouseName = this.propDictionaries.warehouseId.filter(wh => this.form.warehouseId == wh.id).map(wh => wh.name)[0]
        this.$set(this.form, 'warehouseName', warehouseName)

        if (!this.form.requiredUnit) {
          this.$set(this.form, 'requiredUnit', this.form.skuUnit)
          this.$set(this.form, 'requiredRate', 1)
        }
        this.$emit('data-callback', this.form)
        return true
      },
      afterMove: (formData, initData) => {
        this.form = global.utils.jsonClone(formData)
        if (!formData.warehouseId && !!initData.id) { //初始化warehouseId
          this.$set(this.form, 'warehouseId', initData.id)
        }
        // 设置switch 元素默认值
      },
      afterSave: () => {  },
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {
      },
      beforeFlow: (flowButtonKey) => {
        return true
      },
      afterFlow: (flowButtonKey) => {
      },
      beforeClose: () => {
        return true
      }
    })
  },
  props: {
    propParent: Object,
    propInitData: Object,
    propPrimaryKey: String,
    propDictionaries: Object,
    propCurrentRow: Object,
    propViewFormData: Object,
    propReadonly: Boolean,
    propStatus: String,
    propRules: Object
  },
  components: {
    MatrSelect,
    MatrUnitSelect
  },
  computed: {
  }
}
</script>

<style lang="scss" scoped>
  .container {
    width: 100%;
    height: 100%;
  }
</style>
